<template> 
  <div>
    <el-upload
      class="avatar-uploader"
      :action="uploadPath"
      :show-file-list="false"
      :on-success="handleVideoSuccess"
      :before-upload="beforeUploadVideo"
      :on-progress="uploadVideoProcess"
      accept=".mp4,.qlv,.qsv,.ogg,.flv,.avi,.wmv,.rmvb">
      <video v-if="value && videoFlag === false"  :src="value" class="avatar" controls="controls">您的浏览器不支持视频播放</video>
      <i v-else-if="!value && videoFlag === false" class="el-icon-plus avatar-uploader-icon"></i>
      <el-progress v-if="videoFlag" type="circle" :percentage="videoUploadPercent" style="margin-top:30px;"></el-progress>
    </el-upload>
    <p class="text">请保证视频格式正确，且不超过100M</p>
  </div>
</template>
<script>
  import {uploadPath} from '@/api/storage'

  export default {
    name: 'videoUpload',
    props: {
      value: String
    },
    data() {
      return {
        uploadPath,
        dataObj: {
          policy: '',
          signature: '',
          key: '',
          ossaccessKeyId: '',
          dir: '',
          host: ''
        },
        dialogVisible: false,
        videoFlag: false,
        videoUploadPercent: '0%',
      };
    },
    mounted(){

    },
    created(){
      console.log(this)
    },
    methods: {
      emitInput(val) {
        this.$emit('input', val)
      },
      handleRemove(file, fileList) {
        this.emitInput('');
      },
      uploadVideoProcess(event, file, fileList) {
        this.videoFlag = true;
        this.videoUploadPercent =  file.percentage.toFixed(0)
      },
      handleVideoSuccess(res, file) {                               //获取上传图片地址
        this.videoFlag = false;
        this.videoUploadPercent = 0;
        if(res.code == 200){
          this.value = res.data.url;
          this.emitInput(res.data.url);
        }else{
          this.$message.error('视频上传失败，请重新上传！');
        }
      },
      beforeUploadVideo(file) {
        const isLt100M = file.size / 1024 / 1024  < 100;
        if (['video/mp4', 'video/ogg', 'video/flv','video/avi','video/wmv','video/rmvb'].indexOf(file.type) == -1) {
          this.$message.error('请上传正确的视频格式');
          return false;
        }
        if (!isLt100M) {
          this.$message.error('上传视频大小不能超过100MB哦!');
          return false;
        }
      },
    }
  }
</script>
<style>
  .avatar-uploader {
    width: 600px;
  }

  .avatar-uploader .el-upload {
    width: 300px;
    height: 300px;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #20a0ff;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 120px;
    height: 120px;
    line-height: 120px;
    text-align: center;
  }

  .avatar {
    width: 300px;
    height: 300px;
    display: block;
    float: left !important;
  }
</style>


